package cn.anecansaitin.firecrafting.common.net.pack;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public record ScreenButtonPressedToMenu(int button) {

    public static void encode(ScreenButtonPressedToMenu msg, FriendlyByteBuf buffer) {
        buffer.writeInt(msg.button);
    }

    public static ScreenButtonPressedToMenu decode(FriendlyByteBuf buffer) {
        return new ScreenButtonPressedToMenu(buffer.readInt());
    }

    public static void handle(ScreenButtonPressedToMenu packet, Supplier<NetworkEvent.Context> ctx) {
        NetworkEvent.Context context = ctx.get();
        context.enqueueWork(() -> {
            ServerPlayer player = context.getSender();
            if (player != null) {
                player.containerMenu.clickMenuButton(player, packet.button);
            }
        });
        context.setPacketHandled(true);
    }
}
